<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |4.3.2010 Erstellung | Patrik Pfaffenbauer    |
 |_____________________________________________|

******************************************************************************

P3(c) */
class Permission extends Base
{
	var $selectedGroupID;
	
	public function Permission()
	{
		$this->title = $GLOBALS['lang']->GetLanguage("Permission", "title");
		$this->toolbar = true;
		$this->moduleID = 130;
		$this->permission = GetPermission($this->moduleID);
		$this->toolbarClass = "icon-48-permission";
		
		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
	}
	
	public function Add()
	{		
		include("./_tables.php");
		$panel = new Panel();
		
		$panel->AddRow();
		$panel->AddTextBox("name", $GLOBALS['lang']->GetLanguage("Common", "name"), "", 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("description", $GLOBALS['lang']->GetLanguage("Common", "description"), "", 300, 30);
		$panel->AddRow();
		
		$panel->AddHidden("action", "save");
		$panel->AddHidden("option", "permission");
		
		$panel->Close();
	}
	public function Edit()
	{		
		
	}
	
	public function AnalysPost()
	{
		include("./_tables.php");
		
		if($_POST['action'] == "change")
		{
			$this->selectedGroupID = $_POST['usergroup'];
			$this->Paint();
		}
		else if($_POST['action'] == "add")
		{
			$this->Add();
		}
		else if($_POST['action'] == "save")
		{
			$GLOBALS['db']->SaveObject($tblusergroup);
			$this->Paint();
		}
		else if($_POST['action'] = "delete")
		{
			$this->selectedGroupID = $_POST['usergroup'];
			$GLOBALS['db']->Query("DELETE FROM ".$tblusergroup." WHERE id='".$this->selectedGroupID."'");
			$GLOBALS['db']->Query("DELETE FROM ".$tbluser2group." WHERE groupID='".$this->selectedGroupID."'");
			$GLOBALS['db']->Query("DELETE FROM ".$tblpermission." WHERE groupID='".$this->selectedGroupID."'");
			
			$this->selectedGroupID = 1;
			$this->Paint();
		}
		else
		{
			$this->Paint();
		}
	}

	public function Paint()
	{
		include("./_tables.php");
		$res = $GLOBALS['db']->Query("SELECT * FROM ".$tblusergroup);
		
		if($this->selectedGroupID == "")
		{
			$this->selectedGroupID = 1;
		}
		
		if(!$this->permission->editPermission)
			$misc = "readonly";
		else
			$misc = "";
		
		echo 
		'<div id="cpanel">
		<form action="index.php?s='.$_GET['s'].'" method="post" name="adminForm">
		<table id="adminform">
		<tr>
		<td colspan="2" align="text-align:center;">
		<br/>
		</td>
		</tr>
		<tr>
		<td>';
		
		echo $GLOBALS['lang']->GetLanguage("Permission", "user-group").': <select name="usergroup" onchange="Submitform(\'change\')">';
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			if($this->selectedGroupID == $line->id)
			{
				echo '<option value="'.$line->id.'" selected>'.$line->description.'</option>';
			}
			else
			{
			 	echo '<option value="'.$line->id.'">'.$line->description.'</option>';
			}
		}
		
		
		echo '</select></td><td colspan="2"></td><td>'.$GLOBALS['lang']->GetLanguage("Permission", "modules").':</td><tr><td valign="top">
		
		<div id="leftColumn" class="leftColumn">
			<p><b>'.$GLOBALS['lang']->GetLanguage("Permission", "notInGroup").'</b></p>
			<div id="dropContent">';
		$query = "SELECT * FROM ".$viewusernotingroup." WHERE groupID='".$this->selectedGroupID."'";
		$res = $GLOBALS['db']->Query($query);
		$color = true;
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			if($color)
			{
				echo '<div class="dragableBox2" lang="'.$line->userid.'" id="box'.$line->userid.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
			}	
			else
			{
				echo '<div class="dragableBox" lang="'.$line->userid.'" id="box'.$line->userid.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
			}
			
			$color = !$color;
		}
				
			echo '</div>
		</div>
		</td>
		<td valign="top">
			<div id="rightColumn">
			<div id="dropBox" class="dropBox">
				<p><b>'.$GLOBALS['lang']->GetLanguage("Permission", "inGroup").'</b></p>
				<div id="dropContent2">';
			$query2 = "SELECT * FROM ".$viewuseringroup." WHERE groupID='".$this->selectedGroupID."'";	
			$res = $GLOBALS['db']->Query($query2);
			$color = true;
			
			while($line = $GLOBALS['db']->Fetch($res))
			{
				if($color)
				{
					echo '<div class="dragableBox2" lang="'.$line->userid.'" id="box'.$line->userid.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
				}	
				else
				{
					echo '<div class="dragableBox" lang="'.$line->userid.'" id="box'.$line->userid.'" title="'.str_replace("{0}", $line->firstName.' '.$line->lastName, $GLOBALS['lang']->GetLanguage("Permission", "clickToDragDrop")).'">'.$line->firstName.' '.$line->lastName.'</div>';
				}
				$color = !$color;
			}
			
			echo '</div>
		</div>
		<input type="hidden" name="boxchecked" value="1" />
		<input type="hidden" name="action" value="" />
		</td><td width="100px"></td><td valign="top">
		<table class="adminform">';
			
		$res = $GLOBALS['db']->Query("SELECT * FROM ".$tblmodules);
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			$q= "SELECT * FROM ".$tblpermission." WHERE moduleid='".$line->moduleID."' AND groupid='".$this->selectedGroupID."'";
			$res2 = $GLOBALS['db']->Query($q);
			
			if($GLOBALS['db']->NumRow($res2))
			{
				$line2 = $GLOBALS['db']->Fetch($res2);
				
				echo '<tr><td>'.$GLOBALS['lang']->GetLanguage(ucfirst($line->name), "title").'</td>';
				if(PermissionCount($line2) == 4)
				{
					echo ' <td><input type="checkbox" id="full-'.$line->moduleID.'" name="full-'.$line->moduleID.'" value="full-'.$line->moduleID.'" onchange="SetAccess(\'full\', '.$line->moduleID.');" checked '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "fullAccess").'</td>';
				}
				else
				{
					echo ' <td><input type="checkbox" id="full-'.$line->moduleID.'" name="full-'.$line->moduleID.'" value="full-'.$line->moduleID.'" onchange="SetAccess(\'full\', '.$line->moduleID.');" '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "fullAccess").' </td>';
				}
				
				if($line2->specialPermission)
				{
					echo ' <td><input type="checkbox" id="special-'.$line->moduleID.'" name="special-'.$line->moduleID.'" value="special-'.$line->moduleID.'" onchange="SetAccess(\'special\', '.$line->moduleID.');" checked '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "special").' </td>';
				}
				else
				{
					echo ' <td><input type="checkbox" id="special-'.$line->moduleID.'" name="special-'.$line->moduleID.'" value="special-'.$line->moduleID.'" onchange="SetAccess(\'special\', '.$line->moduleID.');" '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "special").' </td>';
				}
				if($line2->deletePermission)
				{
					echo ' <td><input type="checkbox" id="delete-'.$line->moduleID.'" name="delete-'.$line->moduleID.'" value="delete-'.$line->moduleID.'" onchange="SetAccess(\'delete\', '.$line->moduleID.');" checked '.$misc.'/>'.$GLOBALS['lang']->GetLanguage("Common", "delete").'</td>';
				}
				else
				{
					echo ' <td><input type="checkbox" id="delete-'.$line->moduleID.'" name="delete-'.$line->moduleID.'" value="delete-'.$line->moduleID.'" onchange="SetAccess(\'delete\', '.$line->moduleID.');" '.$misc.'/>'.$GLOBALS['lang']->GetLanguage("Common", "delete").'</td>';
				}
				if($line2->editPermission)
				{
					echo ' <td><input type="checkbox" id="write-'.$line->moduleID.'" name="write-'.$line->moduleID.'" value="write-'.$line->moduleID.'" onchange="SetAccess(\'write\', '.$line->moduleID.');" checked '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "write").' </td>';	
				}
				else
				{
					echo ' <td><input type="checkbox" id="write-'.$line->moduleID.'" name="write-'.$line->moduleID.'" value="write-'.$line->moduleID.'" onchange="SetAccess(\'write\', '.$line->moduleID.');" '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "write").' </td>';
				}
				if($line2->readPermission)
				{
					echo ' <td><input type="checkbox" id="read-'.$line->moduleID.'" name="read-'.$line->moduleID.'" value="read-'.$line->moduleID.'" onchange="SetAccess(\'read\', '.$line->moduleID.');" checked '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "read").'</td>';	
				}
				else
				{
					echo ' <td><input type="checkbox" id="read-'.$line->moduleID.'" name="read-'.$line->moduleID.'" value="read-'.$line->moduleID.'" onchange="SetAccess(\'read\', '.$line->moduleID.');" '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "read").'</td>';
				}
			}
			else
			{
				echo '<td>'.$GLOBALS['lang']->GetLanguage(ucfirst($line->name), "title").'</td>';
				echo ' <td><input type="checkbox" id="full-'.$line->moduleID.'" name="full-'.$line->moduleID.'" value="full-'.$line->moduleID.'" onchange="SetAccess(\'full\', '.$line->moduleID.');" '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "fullAccess").'</td>';
				echo ' <td><input type="checkbox" id="special-'.$line->moduleID.'" name="special-'.$line->moduleID.'" value="special-'.$line->moduleID.'" onchange="SetAccess(\'special\', '.$line->moduleID.');" '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "special").'</td>';
				echo ' <td><input type="checkbox" id="delete-'.$line->moduleID.'" name="delete-'.$line->moduleID.'" value="delete-'.$line->moduleID.'" onchange="SetAccess(\'delete\', '.$line->moduleID.');" '.$misc.'/>'.$GLOBALS['lang']->GetLanguage("Common", "delete").'</td>';
				echo ' <td><input type="checkbox" id="write-'.$line->moduleID.'" name="write-'.$line->moduleID.'" value="write-'.$line->moduleID.'" onchange="SetAccess(\'write\', '.$line->moduleID.');" '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "write").'</td>';
				echo ' <td><input type="checkbox" id="read-'.$line->moduleID.'" name="read-'.$line->moduleID.'" value="read-'.$line->moduleID.'" onchange="SetAccess(\'read\', '.$line->moduleID.');" '.$misc.'/> '.$GLOBALS['lang']->GetLanguage("Common", "read").'</td>';
			}
			
			echo '</tr>';
		}
		
		echo '<tr rowspan="2" style="vertical-align:center;"><td colspan="6" style="text-algin:center;"><div id="permissionResult"></div></td></tr></table></td></tr></table></form>';
		
		echo "
		<script type=\"text/javascript\">

			function dropItems(idOfDraggedItem,targetId,x,y)
			{
				var action = '';
				if(targetId=='dropBox')
				{	
					var obj = document.getElementById(idOfDraggedItem);
					if(obj.parentNode.id=='dropContent2')return;		
					document.getElementById('dropContent2').appendChild(obj);
					action = \"add\";
				}
				if(targetId=='leftColumn')
				{
					var obj = document.getElementById(idOfDraggedItem);
					if(obj.parentNode.id=='dropContent')return;	
					document.getElementById('dropContent').appendChild(obj);
					action = \"removed\";
				}
				
				LoadWithJava('./permission.php?action='+action+'&type=groups&userid='+obj.lang+'&groupid=".$this->selectedGroupID."','permissionResult');
			}
			
			function onDragFunction(cloneId,origId)
			{
				var obj = document.getElementById(cloneId);
				obj.style.border='1px solid #000000';
				obj.style.width = '244px';
				
			}

			var dragDropObj = new DHTMLgoodies_dragDrop();
			";
		if($this->permission->editPermission)
		{
			$res = $GLOBALS['db']->Query($query);
			while($line = $GLOBALS['db']->Fetch($res))
			{
				echo "dragDropObj.addSource('box".$line->userid."',true,true,true,false,'onDragFunction');";
			}
			$res = $GLOBALS['db']->Query($query2);
			while($line = $GLOBALS['db']->Fetch($res))
			{
				echo "dragDropObj.addSource('box".$line->userid."',true,true,true,false,'onDragFunction');";
			}
		}
		
		echo "dragDropObj.addTarget('dropBox','dropItems');	
			dragDropObj.addTarget('leftColumn','dropItems');
			dragDropObj.init();
			</script>";
		echo '</div>';
	}
	public function Toolbar()
	{
		if(!$this->allowed)
			return;
			
		PaintDefaultToolbar($this, false);
	}
	
	public function AddCSS()
	{
			
	}
	public function AddJS()
	{
		echo '<script type="text/javascript" src="js/base/drag-drop.js"></script>';
	}
}

?>